/*
<SquaresBoard.java> - Paints a squares' board on a JPanel.
----------------------------------------------------------------
Copyright(C) 2005 Daniel Estévez Fuentes <daniel.ef@gmail.com> 

This  program is free  software; you can redistribute it and/or
modify  it  under the terms of the  GNU  General Public License
as published by the  Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the  hope that it will be useful,
but  WITHOUT ANY WARRANTY;  without even the implied warranty of
MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE.   See the
GNU General Public License for more details.

You  should  have  received  a  copy of the  GNU  General Public
License  along  with  this  program;  if  not, write to the Free
Software  Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA  02111-1307, USA.

-----------------------------------------------------------------
$Log$ 

------------------------------------------------------------------
 */
package practica2.cobbled;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

/**
 * Paints a squares' board on a JPanel.
 * @author   Daniel Estévez Fuentes <daniel.ef@gmail.com>
 * @since    
 * @version  1.0
 */
public class SquaresBoard extends JPanel {

    private int dimension;
    private Color lines;
    private Color[][] color;

    /**
     * Constructor that receives as parameters the dimension (squares
     * per column and row) and the color of the lines between squares.
     * @param dimension int that indicates the squares per column and
     * row.
     * @param lines Color of line between squares.
     */
    public SquaresBoard(int dimension, Color lines) {
        this.dimension = dimension;
        this.lines = lines;
        color = new Color[dimension][dimension];
    }

    /**
     * Performs the painting of squares and lines.
     * @param gr the Graphics of this Cxomponent.
     */
    @Override
    public void paint(Graphics gr) {
        Graphics2D g2d = (Graphics2D) gr;
        for (int i = 0; i < dimension; i++) {
            for (int j = 0; j < dimension; j++) {
                if (color[i][j] == null) {
                    g2d.setPaint(lines);
                } else {
                    g2d.setPaint(color[i][j]);
                }
                g2d.fill(new Rectangle(i * (getWidth() / dimension),
                        j * (getHeight() / dimension),
                        (i + 1) * (getWidth() / dimension),
                        (j + 1) * (getHeight() / dimension)));
                g2d.setPaint(lines);
                g2d.draw(new Rectangle(i * (getWidth() / dimension),
                        j * (getHeight() / dimension),
                        (i + 1) * (getWidth() / dimension),
                        (j + 1) * (getHeight() / dimension)));
            }
        }
    }

    /**
     * Only update this Component calling to paint().
     * @param gr the Graphics of this Component.
     */
    @Override
    public void update(Graphics gr) {
        paint(gr);
    }

    /**
     * Change the Color of the square (x,y).
     * @param x square's column.
     * @param y square's row.
     * @param color a new Color for the square.
     */
    public void setItem(int x, int y, Color color) {
        this.color[x][y] = color;
        repaint();
    }

    /**
     * Change the Color of all the board.
     * @param color matrix Color matrix with dimension length.
     */
    public void setItems(Color[][] color) {
        if (color.length == dimension) {
            this.color = color;
        }
        repaint();
    }

    /**
     * Resets the component
     */
    public void resetBoard() {
        for (int i = 0; i < dimension; i++) {
            for (int j = 0; j < dimension; j++) {
                this.color[i][j] = lines;
            }
        }
        repaint();
    }

    /**
     * Returns the dimension's board.
     * @return the dimension's board.
     */
    public int getDimension() {
        return dimension;
    }

    /**
     * Returns the color's lines of board.
     * @return the color's lines of board.
     */
    public Color getColorLines() {
        return lines;
    }
}
